package com.mycompany.app.managed.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
 
import com.mycompany.app.model.User;
import com.mycompany.app.service.IUserService;
 
/**
 *
 * User Managed Bean
 *
 * @author onlinetechvision.com
 * @since 25 Mar 2012
 * @version 1.0.0
 *commnet
 */

@ManagedBean(name="userMB")
@RequestScoped
public class UserManagedBean implements Serializable {
	protected final Log logger = LogFactory.getLog(getClass());

    /**
	 * 
	 */
	private static final long serialVersionUID = 7824234559406835933L;
	private static final String SUCCESS = "success";
    private static final String ERROR   = "error";
 
    //Spring User Service is injected...
    @ManagedProperty(value="#{userService}")
    IUserService userService;
 
    List<User> userList;
 
    User user=new User();
 
    /**
     * Add User
     *
     * @return String - Response Message
     */
    public String addUser() {
        try {
            getUserService().addUser(user);
            return SUCCESS;
        } catch (DataAccessException e) {
        	logger.error("Fallo", e);
        }   
 
        return ERROR;
    }
 
    /**
     * Reset Fields
     *
     */
    public void reset() {
        user.setId(0);
       
    }
 
    /**
     * Get User List
     *
     * @return List - User List
     */
    public List<User> getUserList() {
        userList = new ArrayList<User>();
        userList.addAll(getUserService().getUsers());
        return userList;
    }
 
    /**
     * Get User Service
     *
     * @return IUserService - User Service
     */
    public IUserService getUserService() {
        return userService;
    }
 
    /**
     * Set User Service
     *
     * @param IUserService - User Service
     */
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }
 
    /**
     * Set User List
     *
     * @param List - User List
     */
    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


	public String resultJSON(){
		return "";
	}
    
    
}
